package com.example.backend.controller;


import com.example.backend.entity.Admin;
import com.example.backend.service.AdminService;
import com.example.backend.util.PwdUtil;
import com.example.backend.util.UUIDUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@Log4j
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/admin")
    public int doAdd(@RequestBody Admin admin){
        log.info("添加管理员：" + admin);
        return adminService.doRegister(admin);
    }
    @PutMapping("/admin")
    public int doUpdate(@RequestBody Admin admin){
        log.info("修改管理员：" + admin);
        return adminService.doUpdate(admin);
    }
    @DeleteMapping("/admin/{id}")
    public int doDelete(@PathVariable("id") String id){
        log.info("删除管理员：" + id);
        return adminService.doDelete(id);
    }
    @GetMapping("/admin/{id}")
    public Admin doGet(@PathVariable("id") String id){
        log.info("查询管理员：" + id);
        return adminService.getAdminById(id);
    }
    @GetMapping("/admin/list")
    public Map<String, Object> listAdmins(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            Admin admin) {

        return adminService.doList(admin, pageNum, pageSize);
    }

    @PostMapping("/admin/login")
    public Map<String, Object> login(@RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();
        try {
            Admin dbAdmin = adminService.login(admin.getName(), admin.getPwd());
            //
            log.info("输入密码加密后：" + PwdUtil.getEncryptedPwd(admin.getPwd()));
            log.info("数据库存储密码：" + (dbAdmin != null ? dbAdmin.getPwd() : "null"));
            if (dbAdmin != null) {
                result.put("success", true);
                result.put("message", "登录成功");
                result.put("token", UUIDUtil.getUUID());
                result.put("admin", dbAdmin);
            } else {
                result.put("success", false);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常");
        }
        return result;
    }

    @PostMapping("/admin/register")
    public Map<String, Object> register(@RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (adminService.selectByAdminname(admin.getName()) != null) {
                result.put("success", false);
                result.put("message", "管理员名称已存在");
                return result;
            }

            int count = adminService.doRegister(admin);   // 里面已加密
            result.put("success", count > 0);
            result.put("message", count > 0 ? "注册成功" : "注册失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常");
        }
        return result;
    }
    //
}